'''
Created on Aug 30, 2012

@author: petrina
'''
from link_indicator import Link_indicator
import logging
from indiutils import Indiutils


class Planning_speed(Link_indicator):
    '''
    Calculates the planning speed for every time interval (using aggregated fcd-data).
    
    The planning speed is a kind of worst case speed. It is something like
    the planning time but with speed. Therefore the planing time is equal 
    to the 5th percentile of the speeds of a interval.
    The indicator calculates the planing speed for each interval.

    unit: kilometers per hour (km/h)
    '''


    def __init__(self, link):
        '''        
        @param link: Link-object (containing valid records and timeseries) 
        '''
        
        Link_indicator.__init__(self, link)
        self._name = 'Planning Speed'
        self._mr_name =  'plan_speed'
        self._unit = 'km/h' if link.records.is_kph else 'm/s'
        #self.unit.__doc__= 'kilometers per hour (hm/h)'
        
        self._create_title(link.id, link.direction)
        
        logging.debug("Planning_speed: created new Planning_speed")

    #----------------------public methods of the class-------------------------

    def calculate(self):
        '''
        @return: planning speed for every time interval as list of Result_values
        '''
        
        logging.info("Planning_speed: starting to calculate the planning speed")
        
        utils = Indiutils() 
        self._values = utils.p5_speed(self._link.timeseries)
        
        logging.debug("Planning_speed: finished calculating the planning speed")
        
        return self._values
    
    
    def plot(self, plotobject):
        '''
        plots the indicator and the speeds of the aggregated fleet data
        '''
        
        self._plot_data(plotobject, 'speed') 